<?php

namespace MoChat\App\Statistic\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use MoChat\App\Client\Model\Client;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use TheSeer\Tokenizer\Exception;
use Hyperf\Command\Annotation\Command;

/**
 * 通话记录数据映射用户
 * @Command
 */
class CallRecordRelationCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('callRecordRelation');
    }

    public function configure()
    {
        parent::configure();
        $this->addOption('clear', 'C', InputArgument::OPTIONAL, '清除redis', 0);
        $this->setDescription('通话记录数据映射任务');
    }

    public function handle()
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(Redis::class);
        $clear = $this->input->getOption('clear');
        if ($clear) {
            $redis->del('CallRecordRelationCommand_key');
        }
        $bool = $redis->get('CallRecordRelationCommand_key');
        if ($bool) {
            $this->line('上一个通话记录数据映射任务脚本程序正在执行中', 'error');
        } else {
            $redis->set('CallRecordRelationCommand_key', true,300);
            Db::beginTransaction();
            try {
                $records = Db::table('call_record')
                    ->where('user_id',0)
                    ->limit(2000)
                    ->select([
                        'id',
                        'caller_id_number',
                        'destination_number',
                        'call_type'
                    ])
                    ->get();
                $records || $records = collect([]);
                $records = $records->toArray();
                if (empty($records)) {
                    throw new \Exception('暂无数据');
                }
                $number = array_merge(array_column($records, 'caller_id_number'), array_column($records, 'destination_number'));
                $number = array_unique($number);
                $users = Db::table('user')
                    ->leftJoin('work_employee as employee', 'employee.log_user_id', '=', 'user.id')
                    ->whereIn('user.extn', $number)
                    ->select([
                        'user.extn',
                        'user.id as user_id',
                        'employee.id as work_employee_id',
                    ])->get();
                $users || $users = collect([]);
                $users = $users->toArray();
                $users=array_column($users,null,'extn');
                foreach ($records as $record) {
                    if ($record->call_type == 'out') {
                        Db::table('call_record')
                            ->where('id',$record->id)
                            ->update([
                                'user_id'=>$users[$record->caller_id_number]->user_id??0,
                                'work_employee_id'=>$users[$record->caller_id_number]->work_employee_id??null,
                            ]);
                        continue;
                    }
                    if ($record->call_type == 'in') {
                        Db::table('call_record')
                            ->where('id',$record->id)
                            ->update([
                                'user_id'=>$users[$record->destination_number]->user_id??0,
                                'work_employee_id'=>$users[$record->destination_number]->work_employee_id??null,
                            ]);
                    }
                }
                Db::commit();
            } catch (\Throwable $throwable) {
                dump($throwable->getMessage());
                dump($throwable->getLine());
                Db::rollBack();
            }
            $redis->del('CallRecordRelationCommand_key');
            $this->line('通话记录数据映射任务脚本处理完成', 'info');
        }
    }
}
